package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/v1/sensitive/")
public class SensitiveController {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 敏感词分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult SensitiveList(@RequestBody SensitiveDto dto) {
        Page wmSensitivePage = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getName()), WmSensitive::getSensitives, dto.getName());
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        Page<WmSensitive> wmSensitivePage1 = wmSensitiveMapper.selectPage(wmSensitivePage, wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) wmSensitivePage1.getTotal());
        pageResponseResult.setData(wmSensitivePage1.getRecords());
        return pageResponseResult;
    }

    /**
     * 保存敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/save")
    public ResponseResult SensitiveSave(@RequestBody AdSensitive adSensitive){
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/update")
    public ResponseResult SensitiveUpate(@RequestBody AdSensitive adSensitive){
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult SensitiveUpate(@PathVariable Long id){
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
